from PyQt5 import QtCore, QtGui, QtWidgets, Qt
from PyQt5.QtWidgets import QTableWidgetItem, QFileDialog, QButtonGroup, QLineEdit, QMessageBox, QApplication
from PyQt5.QtCore import QTranslator
from ui_py.page7_new import Main_Ui
from temp import Excel, ReadExcel
from grap import *
import json
from validation import validation
from classes.engine import Engine
import pathlib
import glob
import os
import re
import shutil
import json
from validation import validation
from event import Event
from expert import Expert
from error import Error
from dataExpert import Data
class Page7(QtWidgets.QWidget, Main_Ui):
    engine = Engine()
    def __init__(self, filename):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)
        self.data = Data()
        with open('lang.json', encoding='utf-8') as file:
            data = json.load(file)
        if data['lang'] != "":
            self.trans = QTranslator(self)
            self.trans.load('translate/' + 'page7')
            _app = QApplication.instance() # получить экземпляр приложения
            _app.installTranslator(self.trans)
        self.radio = 1
        self.basic_file = filename
        self.err = Error()
        if self.basic_file != "":
            self.err.show_page7 = 1 
        self.event = Event()
        self.expert = Expert()
        self.button_by_data = {
            self.radioButton: 1,
            self.radioButton_2: 2
        }
        self.button_by_data2 = {
            self.radioButton_3: 1,
            self.radioButton_4: 2
        }
        self.button_by_data3 = {
            self.radioButton_5: 1,
            self.radioButton_6: 2
        }
        self.group = QButtonGroup()
        self.group2 = QButtonGroup()
        self.group3 = QButtonGroup()
        self.group.setExclusive(False)
        self.group2.setExclusive(False)
        self.group3.setExclusive(False)
        self.group.addButton(self.radioButton)
        self.group.addButton(self.radioButton_2)
        self.group2.addButton(self.radioButton_3)
        self.group2.addButton(self.radioButton_4)
        self.group3.addButton(self.radioButton_5)
        self.group3.addButton(self.radioButton_6)
        self.group.buttonClicked.connect(self.radio_buttons)
        self.group2.buttonClicked.connect(self.radio_buttons2)
        self.group3.buttonClicked.connect(self.radio_buttons3)

        self.checkBox.stateChanged.connect(self.state_1)
        self.checkBox_2.stateChanged.connect(self.state_2)
        self.checkBox_3.stateChanged.connect(self.state_3)
        self.checkBox_4.stateChanged.connect(self.phase_change)
        self.lineEdit.textChanged.connect(self.line1)
        self.lineEdit_2.textChanged.connect(self.line2)
        self.lineEdit_3.textChanged.connect(self.line3)
        self.lineEdit_4.textChanged.connect(self.line4)
        self.lineEdit_5.textChanged.connect(self.line5)
        self.lineEdit_6.textChanged.connect(self.line6)
        self.lineEdit_7.textChanged.connect(self.line7)
        self.lineEdit_8.textChanged.connect(self.line8)
        self.lineEdit_9.textChanged.connect(self.line9)
        self.lineEdit_10.textChanged.connect(self.line10)
        self.lineEdit_11.textChanged.connect(self.line11)
        self.lineEdit_12.textChanged.connect(self.line12)

        self.lineEdit.setEnabled(False)
        self.lineEdit_2.setEnabled(False)
        self.lineEdit_3.setEnabled(False)
        self.lineEdit_4.setEnabled(False)
        self.lineEdit_5.setEnabled(False)
        self.lineEdit_6.setEnabled(False)
        self.lineEdit_7.setEnabled(False)
        self.lineEdit_8.setEnabled(False)
        self.lineEdit_9.setEnabled(False)
        self.lineEdit_10.setEnabled(False)
        self.lineEdit_11.setEnabled(False)
        self.lineEdit_12.setEnabled(False)

        self.radioButton.setEnabled(False)
        self.radioButton_2.setEnabled(False)
        self.radioButton_3.setEnabled(False)
        self.radioButton_4.setEnabled(False)
        self.radioButton_5.setEnabled(False)
        self.radioButton_6.setEnabled(False)
        if self.err.clear:
            self.clear()
        if self.expert.input_mode1 == 1:
            self.radioButton.setChecked(True)
            self.radioButton.setEnabled(True)
            self.radioButton_2.setEnabled(True)
            self.lineEdit.setEnabled(True)
            self.lineEdit_4.setEnabled(True)
            self.lineEdit_5.setEnabled(True)
            self.lineEdit_6.setEnabled(True)
        if self.expert.input_mode1 == 2:
            self.radioButton_2.setChecked(True)
            self.lineEdit.setEnabled(True)
            self.lineEdit_4.setEnabled(True)
        if self.expert.input_mode2 == 1:
            self.radioButton_3.setChecked(True)
            self.radioButton_3.setEnabled(True)
            self.radioButton_4.setEnabled(True)
            self.lineEdit_2.setEnabled(True)
            self.lineEdit_7.setEnabled(True)
            self.lineEdit_8.setEnabled(True)
            self.lineEdit_9.setEnabled(True)
        if self.expert.input_mode2 == 2:
            self.radioButton_4.setChecked(True)
            self.lineEdit_2.setEnabled(True)
            self.lineEdit_7.setEnabled(True)
        if self.expert.input_mode3 == 1:
            self.radioButton_5.setChecked(True)
            self.radioButton_5.setEnabled(True)
            self.radioButton_6.setEnabled(True)
            self.lineEdit_3.setEnabled(True)
            self.lineEdit_10.setEnabled(True)
            self.lineEdit_11.setEnabled(True)
            self.lineEdit_12.setEnabled(True)
        if self.expert.input_mode3 == 2:
            self.radioButton_6.setChecked(True)
            self.lineEdit_3.setEnabled(True)
            self.lineEdit_10.setEnabled(True)
        try:
            with open('data/app.json') as file:
                da = json.load(file)
            if da['phase']:
                self.checkBox_4.setChecked(True)
        except:pass
        if len(self.expert.modes) != 0:
            for i in self.expert.modes:
                if i == 2:
                    self.checkBox.blockSignals(True)
                    self.checkBox.setChecked(True)
                    self.radioButton.setEnabled(True)
                    self.radioButton_2.setEnabled(True)
                    self.checkBox.blockSignals(False)
                    try:
                        self.lineEdit.setText(self.expert.dict_mode1['nn'])
                    except:
                        pass
                    try:
                        self.lineEdit_4.setText(self.expert.dict_mode1['pm1'])
                    except:
                        pass
                    try:
                        self.lineEdit_5.setText(self.expert.dict_mode1['pn1'])
                    except:
                        pass
                    try:
                        self.lineEdit_6.setText(self.expert.dict_mode1['pz1'])
                    except:
                        pass
                if i == 3:
                    self.checkBox_2.blockSignals(True)
                    self.checkBox_2.setChecked(True)
                    self.radioButton_3.setEnabled(True)
                    self.radioButton_4.setEnabled(True)
                    self.checkBox_2.blockSignals(False)
                    try:
                        self.lineEdit_2.setText(self.expert.dict_mode2['nn'])
                    except:
                        pass
                    try:
                        self.lineEdit_7.setText(self.expert.dict_mode2['pm1'])
                    except:
                        pass
                    try:
                        self.lineEdit_8.setText(self.expert.dict_mode2['pn1'])
                    except:
                        pass
                    try:
                        self.lineEdit_9.setText(self.expert.dict_mode2['pz1'])
                    except:
                        pass
                if i == 4:
                    self.checkBox_3.blockSignals(True)
                    self.checkBox_3.setChecked(True)
                    self.radioButton_5.setEnabled(True)
                    self.radioButton_6.setEnabled(True)
                    self.checkBox_3.blockSignals(False)
                    try:
                        self.lineEdit_3.setText(self.expert.dict_mode3['nn'])
                    except:
                        pass
                    try:
                        self.lineEdit_10.setText(self.expert.dict_mode3['pm1'])
                    except:
                        pass
                    try:
                        self.lineEdit_11.setText(self.expert.dict_mode3['pn1'])
                    except:
                        pass
                    try:
                        self.lineEdit_12.setText(self.expert.dict_mode3['pz1'])
                    except:
                        pass
        if self.expert.from_file:
            self.lineEdit.blockSignals(True)
            self.lineEdit_2.blockSignals(True)
            self.lineEdit_3.blockSignals(True)
            self.lineEdit_4.blockSignals(True)
            self.lineEdit_5.blockSignals(True)
            self.lineEdit_6.blockSignals(True)
            self.lineEdit_7.blockSignals(True)
            self.lineEdit_8.blockSignals(True)
            self.lineEdit_9.blockSignals(True)
            self.lineEdit_10.blockSignals(True)
            self.lineEdit_11.blockSignals(True)
            self.lineEdit_12.blockSignals(True)
            with open('data/app.json') as file:
                data = json.load(file)
            if "input_mode1" in data.keys():
                if data['input_mode1'] == 1:
                    excel = ReadExcel(glob.glob("data/mode1/*.xlsx")[0])
                    self.expert.dict_mode1 = excel.getModeParam()
                    print(self.expert.dict_mode1)
                    if 'nm' in self.expert.dict_mode1 and self.expert.dict_mode1['nm'] != None:
                        self.lineEdit.setText(str(self.expert.dict_mode1['nm']))
                    else:
                        self.lineEdit.setText("")
                    if 'pm1' in self.expert.dict_mode1 and self.expert.dict_mode1['pm1'] != None:
                        self.lineEdit_4.setText(str(self.expert.dict_mode1['pm1']))
                    else:
                        self.lineEdit_4.setText("")
                    if 'pn1' in self.expert.dict_mode1 and self.expert.dict_mode1['pn1'] != None:
                        self.lineEdit_5.setText(str(self.expert.dict_mode1['pn1']))
                    else:
                        self.lineEdit_5.setText("")
                    if 'pz1' in self.expert.dict_mode1 and self.expert.dict_mode1['pz1'] != None:
                        self.lineEdit_6.setText(str(self.expert.dict_mode1['pz1']))
                    else:
                        self.lineEdit_6.setText("")
                    self.checkBox.setChecked(True)
                    self.lineEdit.setEnabled(True)
                    self.lineEdit_4.setEnabled(True)
                    self.lineEdit_5.setEnabled(True)
                    self.lineEdit_6.setEnabled(True)
                    self.radioButton.setChecked(True)
                elif data['input_mode1'] == 2:
                    excel = ReadExcel(glob.glob("data/mode1/*.xlsx")[0])
                    self.expert.dict_mode1 = excel.getModeParam()
                    if 'nm' in self.expert.dict_mode1 and self.expert.dict_mode1['nm'] != None:
                        self.lineEdit.setText(str(self.expert.dict_mode1['nm']))
                    else:
                        self.lineEdit.setText("")
                        self.lineEdit_5.setText("")
                        self.lineEdit_6.setText("")
                    if 'pm1' in self.expert.dict_mode1 and self.expert.dict_mode1['pm1'] != None:
                        self.lineEdit_4.setText(str(self.expert.dict_mode1['pm1']))
                    else:
                        self.lineEdit_4.setText("")
                    self.checkBox.setChecked(True)
                    self.lineEdit.setEnabled(True)
                    self.lineEdit_4.setEnabled(True)
                    self.radioButton_2.setChecked(True)
            if "input_mode2" in data.keys():
                if data['input_mode2'] == 1:
                    excel = ReadExcel(glob.glob("data/mode2/*.xlsx")[0])
                    self.expert.dict_mode2 = excel.getModeParam()
                    if 'nm' in self.expert.dict_mode2 and self.expert.dict_mode2['nm'] != None:
                        self.lineEdit_2.setText(str(self.expert.dict_mode2['nm']))
                    else:
                        self.lineEdit_2.setText("")
                    if 'pm1' in self.expert.dict_mode2 and self.expert.dict_mode2['pm1'] != None:
                        self.lineEdit_7.setText(str(self.expert.dict_mode2['pm1']))
                    else:
                        self.lineEdit_7.setText("")
                    if 'pn1' in self.expert.dict_mode2 and self.expert.dict_mode2['pn1'] != None:
                        self.lineEdit_8.setText(str(self.expert.dict_mode2['pn1']))
                    else:
                        self.lineEdit_8.setText("")
                    if 'pz1' in self.expert.dict_mode2 and self.expert.dict_mode2['pz1'] != None:
                        self.lineEdit_9.setText(str(self.expert.dict_mode2['pz1']))
                    else:
                        self.lineEdit_9.setText("")

                    self.checkBox_2.setChecked(True)
                    self.lineEdit_2.setEnabled(True)
                    self.lineEdit_7.setEnabled(True)
                    self.lineEdit_8.setEnabled(True)
                    self.lineEdit_9.setEnabled(True)
                    self.radioButton_3.setChecked(True)
                elif data['input_mode2'] == 2:
                    excel = ReadExcel(glob.glob("data/mode2/*.xlsx")[0])
                    self.expert.dict_mode2 = excel.getModeParam()
                    if 'nm' in self.expert.dict_mode2 and self.expert.dict_mode2['nm'] != None:
                        self.lineEdit_2.setText(str(self.expert.dict_mode2['nm']))
                    else:
                        self.lineEdit_2.setText("")
                    if 'pm1' in self.expert.dict_mode2 and self.expert.dict_mode2['pm1'] != None:
                        self.lineEdit_7.setText(str(self.expert.dict_mode2['pm1']))
                    else:
                        self.lineEdit_7.setText("")
                        self.lineEdit_8.setText("")
                        self.lineEdit_9.setText("")
                    self.checkBox_2.setChecked(True)
                    self.lineEdit_2.setEnabled(True)
                    self.lineEdit_7.setEnabled(True)
                    self.radioButton_4.setChecked(True)
            if "input_mode3" in data.keys():
                if data['input_mode3'] == 1:
                    excel = ReadExcel(glob.glob("data/mode3/*.xlsx")[0])
                    self.expert.dict_mode3 = excel.getModeParam()
                    if 'nm' in self.expert.dict_mode3 and self.expert.dict_mode3['nm'] != None:
                        self.lineEdit_3.setText(str(self.expert.dict_mode3['nm']))
                    else:
                        self.lineEdit_3.setText("")
                    if 'pm1' in self.expert.dict_mode3 and self.expert.dict_mode3['pm1'] != None:
                        self.lineEdit_10.setText(str(self.expert.dict_mode3['pm1']))
                    else:
                        self.lineEdit_10.setText("")
                    if 'pn1' in self.expert.dict_mode3 and self.expert.dict_mode3['pn1'] != None:
                        self.lineEdit_11.setText(str(self.expert.dict_mode3['pn1']))
                    else:
                        self.lineEdit_11.setText("")
                    if 'pz1' in self.expert.dict_mode3 and self.expert.dict_mode3['pz1'] != None:
                        self.lineEdit_12.setText(str(self.expert.dict_mode3['pz1']))
                    else:
                        self.lineEdit_12.setText("")

                    self.checkBox_3.setChecked(True)
                    self.lineEdit_3.setEnabled(True)
                    self.lineEdit_10.setEnabled(True)
                    self.lineEdit_11.setEnabled(True)
                    self.lineEdit_12.setEnabled(True)
                    self.radioButton_5.setChecked(True)
                elif data['input_mode3'] == 2:
                    excel = ReadExcel(glob.glob("data/mode3/*.xlsx")[0])
                    self.expert.dict_mode3 = excel.getModeParam()
                    if 'nm' in self.expert.dict_mode3 and self.expert.dict_mode3['nm'] != None:
                        self.lineEdit_3.setText(str(self.expert.dict_mode3['nm']))
                    else:
                        self.lineEdit_3.setText("")
                        self.lineEdit_11.setText("")
                        self.lineEdit_12.setText("")
                    if 'pm1' in self.expert.dict_mode3 and self.expert.dict_mode3['pm1'] != None:
                        self.lineEdit_10.setText(str(self.expert.dict_mode3['pm1']))
                    else:
                        self.lineEdit_10.setText("")

                    self.checkBox_3.setChecked(True)
                    self.lineEdit_3.setEnabled(True)
                    self.lineEdit_10.setEnabled(True)
                    self.radioButton_6.setChecked(True)
            # try:
            #     with open('data/app.json') as file:
            #         data = json.load(file)
            #     for i in data.keys():
            #         if i == "input_mode1":
            #             if data[i] == 1:
            #                 self.radioButton.setChecked(True)
            #             else:
            #                 self.radioButton_2.setChecked(True)
            #         if i == "input_mode2":
            #             if data[i] == 1:
            #                 self.radioButton_3.setChecked(True)
            #             else:
            #                 self.radioButton_4.setChecked(True)
            #         if i == "input_mode3":
            #             if data[i] == 1:
            #                 self.radioButton_5.setChecked(True)
            #             else:
            #                 self.radioButton_6.setChecked(True)
                    
            # except:
            #     pass
            self.lineEdit.blockSignals(False)
            self.lineEdit_2.blockSignals(False)
            self.lineEdit_3.blockSignals(False)
            self.lineEdit_4.blockSignals(False)
            self.lineEdit_5.blockSignals(False)
            self.lineEdit_6.blockSignals(False)
            self.lineEdit_7.blockSignals(False)
            self.lineEdit_8.blockSignals(False)
            self.lineEdit_9.blockSignals(False)
            self.lineEdit_10.blockSignals(False)
            self.lineEdit_11.blockSignals(False)
            self.lineEdit_12.blockSignals(False)
    def line1(self, line):
        self.find_path(1)
        # path = glob.glob('data/mode1/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'nn', self.data.excel):
            self.expert.dict_mode1['nn'] = line
            self.expert.dict_mode1['nm'] = line
            self.lineEdit.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['nn1'] > 0:
                    del self.err.error_dict['nn1']
            except:
                pass
        else:
            self.lineEdit.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['nn1'] = 7
            self.err.page = 7
        # excel.updateCell('nn', line, path)
        # excel.updateCell('nm', line, path)
    def line2(self, line):
        self.find_path(2)
        # path = glob.glob('data/mode2/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'nn', self.data.excel):
            self.expert.dict_mode2['nn'] = line
            self.expert.dict_mode2['nm'] = line
            self.lineEdit_2.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['nn2'] > 0:
                    del self.err.error_dict['nn2']
            except:
                pass
        else:
            self.lineEdit_2.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['nn2'] = 7
            self.err.page = 7
        # excel.updateCell('nn', line, path)
        # excel.updateCell('nm', line, path)
    def line3(self, line):
        self.find_path(3)
        # path = glob.glob('data/mode3/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'nn', self.data.excel):
            self.expert.dict_mode3['nn'] = line
            self.expert.dict_mode3['nm'] = line
            self.lineEdit_3.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['nn3'] > 0:
                    del self.err.error_dict['nn3']
            except:
                pass
        else:
            self.lineEdit_3.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['nn3'] = 7
            self.err.page = 7
        # excel.updateCell('nn', line, path)
        # excel.updateCell('nm', line, path)
    def line4(self, line):
        self.find_path(1)
        # path = glob.glob('data/mode1/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'pm1', self.data.excel):
            self.expert.dict_mode1['pm1'] = line
            self.lineEdit_4.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['pm11'] > 0:
                    del self.err.error_dict['pm11']
            except:
                pass
        else:
            self.lineEdit_4.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['pm11'] = 7
            self.err.page = 7
        # excel.updateCell('pm1', line, path)
    def line5(self, line):
        self.find_path(1)
        # path = glob.glob('data/mode1/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'pn1', self.data.excel):
            self.expert.dict_mode1['pn1'] = line
            self.expert.dict_mode1['pn2'] = line
            self.lineEdit_5.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['pn11'] > 0:
                    del self.err.error_dict['pn11']
            except:
                pass
        else:
            self.lineEdit_5.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['pn11'] = 7
            self.err.page = 7
        # excel.updateCell('pn1', line, path)
        # excel.updateCell('pn2', line, path)
    def line6(self, line):
        self.find_path(1)
        # path = glob.glob('data/mode1/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'pz1', self.data.excel):
            self.expert.dict_mode1['pz1'] = line
            self.expert.dict_mode1['pz2'] = line
            self.lineEdit_6.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['pz11'] > 0:
                    del self.err.error_dict['pz11']
            except:
                pass
        else:
            self.lineEdit_6.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['pz11'] = 7
            self.err.page = 7
        # excel.updateCell('nn', line, path)
        # excel.updateCell('nm', line, path)
    def line7(self, line):
        self.find_path(2)
        # path = glob.glob('data/mode2/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'pm1', self.data.excel):
            self.expert.dict_mode2['pm1'] = line
            self.lineEdit_7.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['pm12'] > 0:
                    del self.err.error_dict['pm12']
            except:
                pass
        else:
            self.lineEdit_7.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['pm12'] = 7
            self.err.page = 7
        # excel.updateCell('pm1', line, path)
    def line8(self, line):
        self.find_path(2)
        # path = glob.glob('data/mode2/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'pn1', self.data.excel):
            self.expert.dict_mode2['pn1'] = line
            self.expert.dict_mode2['pn2'] = line
            self.lineEdit_8.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['pn12'] > 0:
                    del self.err.error_dict['pn12']
            except:
                pass
        else:
            self.lineEdit_8.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['pn12'] = 7
            self.err.page = 7
        # excel.updateCell('pn1', line, path)
        # excel.updateCell('pn2', line, path)
    def line9(self, line):
        self.find_path(2)
        # path = glob.glob('data/mode2/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'pz1', self.data.excel):
            self.expert.dict_mode2['pz1'] = line
            self.expert.dict_mode2['pz2'] = line
            self.lineEdit_9.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['pz12'] > 0:
                    del self.err.error_dict['pz12']
            except:
                pass
        else:
            self.lineEdit_9.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['pz12'] = 7
            self.err.page = 7
        # excel.updateCell('pn1', line, path)
        # excel.updateCell('pn2', line, path)
    def line10(self, line):
        self.find_path(3)
        # path = glob.glob('data/mode3/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'pm1', self.data.excel):
            self.expert.dict_mode3['pm1'] = line
            self.lineEdit_10.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['pm13'] > 0:
                    del self.err.error_dict['pm13']
            except:
                pass
        else:
            self.lineEdit_10.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['pm13'] = 7
            self.err.page = 7
        # excel.updateCell('pm1', line, path)
    def line11(self, line):
        self.find_path(3)
        # path = glob.glob('data/mode3/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'pn1', self.data.excel):
            self.expert.dict_mode3['pn1'] = line
            self.expert.dict_mode3['pn2'] = line
            self.lineEdit_11.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['pn13'] > 0:
                    del self.err.error_dict['pn13']
            except:
                pass
        else:
            self.lineEdit_11.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['pn13'] = 7
            self.err.page = 7
        # excel.updateCell('pn1', line, path)
        # excel.updateCell('pn2', line, path)
    def line12(self, line):
        self.find_path(3)
        # path = glob.glob('data/mode3/*.xlsx')[0]
        # excel = ReadExcel(path)
        if validation(line, 'pz1', self.data.excel):
            self.expert.dict_mode3['pz1'] = line
            self.expert.dict_mode3['pz2'] = line
            self.lineEdit_12.setStyleSheet("QLineEdit {background-color: #70FF7F;}")
            try:
                if self.err.error_dict['pz13'] > 0:
                    del self.err.error_dict['pz13']
            except:
                pass
        else:
            self.lineEdit_12.setStyleSheet("QLineEdit {background-color: #FF9898;}")
            self.err.error_dict['pz13'] = 7
            self.err.page = 7
        # excel.updateCell('pn1', line, path)
        # excel.updateCell('pn2', line, path)
    # def line13(self, line):
    #     # path = glob.glob('data/*.xlsx')[0]
    #     # excel = ReadExcel(path)
    #     self.expert.dict_nonmode['nn'] = line
    #     self.expert.dict_nonmode['nm'] = line
    #     # excel.updateCell('nn', line, path)
    #     # excel.updateCell('nm', line, path)
    # def line14(self, line):
    #     # path = glob.glob('data/*.xlsx')[0]
    #     # excel = ReadExcel(path)
    #     self.expert.dict_nonmode['pn1'] = line
    #     self.expert.dict_nonmode['pn2'] = line
    #     # excel.updateCell('pn1', line, path)
    #     # excel.updateCell('pn2', line, path)
    def state_1(self, state):
        if state:
            self.create_folder(1)
            self.expert.modes.append(2)
            self.radioButton.setEnabled(True)
            self.radioButton_2.setEnabled(True)
        else:
            try:
                self.expert.input_mode1 = 0
                with open('data/app.json') as file:
                    dataf = json.load(file)
                del dataf['input_mode1']
                with open('data/app.json', 'w') as file:
                    json.dump(dataf, file)
            except:pass
            self.delete_folder(1)
            self.expert.modes.remove(2)
            self.radioButton.setEnabled(False)
            self.radioButton_2.setEnabled(False)
            self.radioButton.setChecked(False)
            self.radioButton_2.setChecked(False)
            self.lineEdit.blockSignals(True)
            self.lineEdit_4.blockSignals(True)
            self.lineEdit_5.blockSignals(True)
            self.lineEdit_6.blockSignals(True)
            self.lineEdit.clear()
            self.lineEdit.setEnabled(False)
            self.lineEdit_4.clear()
            self.lineEdit_4.setEnabled(False)
            self.lineEdit_5.clear()
            self.lineEdit_5.setEnabled(False)
            self.lineEdit_6.clear()
            self.lineEdit_6.setEnabled(False)
            self.lineEdit.blockSignals(False)
            self.lineEdit_4.blockSignals(False)
            self.lineEdit_5.blockSignals(False)
            self.lineEdit_6.blockSignals(False)
    def state_2(self, state):
        if state:
            self.create_folder(2)
            self.expert.modes.append(3)
            self.radioButton_3.setEnabled(True)
            self.radioButton_4.setEnabled(True)
        else:
            try:
                self.expert.input_mode2 = 0
                with open('data/app.json') as file:
                    dataf = json.load(file)
                del dataf['input_mode2']
                with open('data/app.json', 'w') as file:
                    json.dump(dataf, file)
            except:pass
            self.delete_folder(2)
            self.expert.modes.remove(3)
            self.radioButton_3.setEnabled(False)
            self.radioButton_4.setEnabled(False)
            self.radioButton_3.setChecked(False)
            self.radioButton_4.setChecked(False)
            self.lineEdit_2.blockSignals(True)
            self.lineEdit_7.blockSignals(True)
            self.lineEdit_8.blockSignals(True)
            self.lineEdit_9.blockSignals(True)
            self.lineEdit_2.clear()
            self.lineEdit_2.setEnabled(False)
            self.lineEdit_7.clear()
            self.lineEdit_7.setEnabled(False)
            self.lineEdit_8.clear()
            self.lineEdit_8.setEnabled(False)
            self.lineEdit_9.clear()
            self.lineEdit_9.setEnabled(False)
            self.lineEdit_2.blockSignals(False)
            self.lineEdit_7.blockSignals(False)
            self.lineEdit_8.blockSignals(False)
            self.lineEdit_9.blockSignals(False)
    def state_3(self, state):
        if state:
            self.create_folder(3)
            self.expert.modes.append(4)
            self.radioButton_5.setEnabled(True)
            self.radioButton_6.setEnabled(True)
        else:
            try:
                self.expert.input_mode3 = 0
                with open('data/app.json') as file:
                    dataf = json.load(file)
                del dataf['input_mode3']
                with open('data/app.json', 'w') as file:
                    json.dump(dataf, file)
            except:pass
            self.delete_folder(3)
            self.expert.modes.remove(4)
            self.radioButton_5.setEnabled(False)
            self.radioButton_6.setEnabled(False)
            self.radioButton_5.setChecked(False)
            self.radioButton_6.setChecked(False)
            self.lineEdit_3.blockSignals(True)
            self.lineEdit_10.blockSignals(True)
            self.lineEdit_11.blockSignals(True)
            self.lineEdit_12.blockSignals(True)
            self.lineEdit_3.clear()
            self.lineEdit_3.setEnabled(False)
            self.lineEdit_10.clear()
            self.lineEdit_10.setEnabled(False)
            self.lineEdit_11.clear()
            self.lineEdit_11.setEnabled(False)
            self.lineEdit_12.clear()
            self.lineEdit_12.setEnabled(False)
            self.lineEdit_3.blockSignals(False)
            self.lineEdit_10.blockSignals(False)
            self.lineEdit_11.blockSignals(False)
            self.lineEdit_12.blockSignals(False)

    def radio_buttons(self, button):
        with open('data/app.json', 'r') as file:
            data = json.load(file)
        for i in self.group.buttons():
            if i is not button:
                i.setChecked(False)
            else:
                if self.button_by_data[i] == 2:
                    file, _ = QFileDialog.getOpenFileName(self, "Open File", ".", "Excel Files (*.xlsx);;All Files (*)")
                    if file:
                        try:
                            excel = Excel(file)
                            arr = excel.ParseFromFile()
                            arr = [i*10.197162 for i in arr]
                            if self.expert.change_phase == True:
                                arr = self.phase_shift_360(arr)
                            for st in self.expert.paths_to_res:
                                if re.search(f"mode1", st):
                                    excel.ParseToFile(arr, st)
                            self.lineEdit.setEnabled(True)
                            self.lineEdit_4.setEnabled(True)
                            self.lineEdit_5.setEnabled(False)
                            self.lineEdit_6.setEnabled(False)
                            self.lineEdit.blockSignals(True)
                            self.lineEdit_4.blockSignals(True)
                            self.lineEdit_5.blockSignals(True)
                            self.lineEdit_6.blockSignals(True)
                            self.lineEdit.clear()
                            self.lineEdit_4.clear()
                            self.lineEdit_5.clear()
                            self.lineEdit_6.clear()
                            self.lineEdit.blockSignals(False)
                            self.lineEdit_4.blockSignals(False)
                            self.lineEdit_5.blockSignals(False)
                            self.lineEdit_6.blockSignals(False)
                        except:
                            msg = QMessageBox()
                            msg.critical(None, "Ошибка", "Загрузите файл или проверьте его корректность")
                            self.radioButton_2.setChecked(False)
                        data['input_mode1'] = 2
                        self.expert.input_mode1 = 2
                    else:
                        self.radioButton.setChecked(False)
                        self.radioButton_2.setChecked(False)
                else:
                    data['input_mode1'] = 1
                    self.expert.input_mode1 = 1
                    self.lineEdit.setEnabled(True)
                    self.lineEdit_4.setEnabled(True)
                    self.lineEdit_5.setEnabled(True)
                    self.lineEdit_6.setEnabled(True)
                    self.lineEdit.blockSignals(True)
                    self.lineEdit_4.blockSignals(True)
                    self.lineEdit_5.blockSignals(True)
                    self.lineEdit_6.blockSignals(True)
                    self.lineEdit.clear()
                    self.lineEdit_4.clear()
                    self.lineEdit_5.clear()
                    self.lineEdit_6.clear()
                    self.lineEdit.blockSignals(False)
                    self.lineEdit_4.blockSignals(False)
                    self.lineEdit_5.blockSignals(False)
                    self.lineEdit_6.blockSignals(False)
        with open('data/app.json', 'w') as file:
            json.dump(data, file)
    def radio_buttons2(self, button):
        with open('data/app.json', 'r') as file:
            data = json.load(file)
        for i in self.group2.buttons():
            if i is not button:
                i.setChecked(False)
            else:
                if self.button_by_data2[i] == 2:
                    file, _ = QFileDialog.getOpenFileName(self, "Open File", ".", "Excel Files (*.xlsx);;All Files (*)")
                    if file:
                        try:
                            excel = Excel(file)
                            arr = excel.ParseFromFile()
                            arr = [i*10.197162 for i in arr]
                            if self.expert.change_phase == True:
                                arr = self.phase_shift_360(arr)
                            for st in self.expert.paths_to_res:
                                if re.search(f"mode2", st):
                                    excel.ParseToFile(arr, st)
                            self.lineEdit_2.setEnabled(True)
                            self.lineEdit_7.setEnabled(True)
                            self.lineEdit_8.setEnabled(False)
                            self.lineEdit_9.setEnabled(False)
                            self.lineEdit_2.blockSignals(True)
                            self.lineEdit_7.blockSignals(True)
                            self.lineEdit_8.blockSignals(True)
                            self.lineEdit_9.blockSignals(True)
                            self.lineEdit_2.clear()
                            self.lineEdit_7.clear()
                            self.lineEdit_8.clear()
                            self.lineEdit_9.clear()
                            self.lineEdit_2.blockSignals(False)
                            self.lineEdit_7.blockSignals(False)
                            self.lineEdit_8.blockSignals(False)
                            self.lineEdit_9.blockSignals(False)
                        except:
                            msg = QMessageBox()
                            msg.critical(None, "Ошибка", "Загрузите файл или проверьте его корректность")
                            self.radioButton_4.setChecked(False)
                        data['input_mode2'] = 2
                        self.expert.input_mode2 = 2
                    else:
                        self.radioButton_3.setChecked(False)
                        self.radioButton_4.setChecked(False)
                else:
                    data['input_mode2'] = 1
                    self.expert.input_mode2 = 1
                    self.lineEdit_2.setEnabled(True)
                    self.lineEdit_7.setEnabled(True)
                    self.lineEdit_8.setEnabled(True)
                    self.lineEdit_9.setEnabled(True)
                    self.lineEdit_2.blockSignals(True)
                    self.lineEdit_7.blockSignals(True)
                    self.lineEdit_8.blockSignals(True)
                    self.lineEdit_9.blockSignals(True)
                    self.lineEdit_2.clear()
                    self.lineEdit_7.clear()
                    self.lineEdit_8.clear()
                    self.lineEdit_9.clear()
                    self.lineEdit_2.blockSignals(False)
                    self.lineEdit_7.blockSignals(False)
                    self.lineEdit_8.blockSignals(False)
                    self.lineEdit_9.blockSignals(False)
        with open('data/app.json', 'w') as file:
            json.dump(data, file)
    def radio_buttons3(self, button):
        with open('data/app.json', 'r') as file:
            data = json.load(file)
        for i in self.group3.buttons():
            if i is not button:
                i.setChecked(False)
            else:
                if self.button_by_data3[i] == 2:
                    file, _ = QFileDialog.getOpenFileName(self, "Open File", ".", "Excel Files (*.xlsx);;All Files (*)")
                    if file:
                        try:
                            excel = Excel(file)
                            arr = excel.ParseFromFile()
                            arr = [i*10.197162 for i in arr]
                            if self.expert.change_phase == True:
                                arr = self.phase_shift_360(arr)
                            for st in self.expert.paths_to_res:
                                if re.search(f"mode3", st):
                                    excel.ParseToFile(arr, st)
                            self.lineEdit_3.setEnabled(True)
                            self.lineEdit_10.setEnabled(True)
                            self.lineEdit_11.setEnabled(False)
                            self.lineEdit_12.setEnabled(False)
                            self.lineEdit_3.blockSignals(True)
                            self.lineEdit_10.blockSignals(True)
                            self.lineEdit_11.blockSignals(True)
                            self.lineEdit_12.blockSignals(True)
                            self.lineEdit_3.clear()
                            self.lineEdit_10.clear()
                            self.lineEdit_11.clear()
                            self.lineEdit_12.clear()
                            self.lineEdit_3.blockSignals(False)
                            self.lineEdit_10.blockSignals(False)
                            self.lineEdit_11.blockSignals(False)
                            self.lineEdit_12.blockSignals(False)
                        except:
                            msg = QMessageBox()
                            msg.critical(None, "Ошибка", "Загрузите файл или проверьте его корректность")
                            self.radioButton_6.setChecked(False)
                        data['input_mode3'] = 2
                        self.expert.input_mode3 = 2
                    else:
                        self.radioButton_5.setChecked(False)
                        self.radioButton_6.setChecked(False)
                else:
                    data['input_mode3'] = 1
                    self.expert.input_mode3 = 1
                    self.lineEdit_3.setEnabled(True)
                    self.lineEdit_10.setEnabled(True)
                    self.lineEdit_11.setEnabled(True)
                    self.lineEdit_12.setEnabled(True)
                    self.lineEdit_3.blockSignals(True)
                    self.lineEdit_10.blockSignals(True)
                    self.lineEdit_11.blockSignals(True)
                    self.lineEdit_12.blockSignals(True)
                    self.lineEdit_3.clear()
                    self.lineEdit_10.clear()
                    self.lineEdit_11.clear()
                    self.lineEdit_12.clear()
                    self.lineEdit_3.blockSignals(False)
                    self.lineEdit_10.blockSignals(False)
                    self.lineEdit_11.blockSignals(False)
                    self.lineEdit_12.blockSignals(False)
        with open('data/app.json', 'w') as file:
            json.dump(data, file)

    def find_path(self, mode):
        if not os.path.isdir(f'data/mode{mode}'):
            os.mkdir(f'data/mode{mode}')
            self.expert.paths_to_folders.append(f"data/mode{mode}")
    
    def create_folder(self, mode):
        if not os.path.isdir(f'data\mode{mode}'):
            os.mkdir(f'data/mode{mode}')
            shutil.copy(self.basic_file, f'data/mode{mode}')
            word = '.xlsx'
            pattern = f"{f'data/mode{mode}'}/*{word}*"
            file = glob.glob(pattern)
            self.filename = file[0][:-5] + "_res.xlsx"
            os.rename(file[0], self.filename)
            self.expert.paths_to_folders.append(f"data\mode{mode}")
            self.expert.paths_to_res.append(glob.glob(f"data\mode{mode}\\*.xlsx")[0])
    def delete_folder(self, mode):
        for st in self.expert.paths_to_folders:
            if re.findall(f"mode{mode}", str(st)):
                self.expert.paths_to_folders.remove(st)
                shutil.rmtree(f'data/mode{mode}')
        for st in self.expert.paths_to_res:
            if re.findall(f"mode{mode}", str(st)):
                self.expert.paths_to_res.remove(st)
    # def load_file(self):
    #     match self.radio:
    #         case 2:
    #             file, _ = QFileDialog.getOpenFileName(self, "Open File", ".", "Excel Files (*.xlsx);;All Files (*)")
    #             if file:
    #                 excel = Excel(file)
    #                 arr = excel.ParseFromFile()
    #                 excel.ParseToFile(arr, self.filename)
    #                 self.label.setText(file)
    #         case 3:
    #             file, _ = QFileDialog.getOpenFileName(self, "Open File", ".", "Excel Files (*.xlsx);;All Files (*)")
    #             if file:
    #                 excel = Excel(file)
    #                 arr = excel.ParseFromFile()
    #                 excel.ParseToFile(arr, self.filename)
    #                 self.label_2.setText(file)
    #         case 4:
    #             file, _ = QFileDialog.getOpenFileName(self, "Open File", ".", "Excel Files (*.xlsx);;All Files (*)")
    #             if file:
    #                 excel = Excel(file)
    #                 arr = excel.ParseFromFile()
    #                 excel.ParseToFile(arr, self.filename)
    #                 self.label_3.setText(file)
    # def radio_buttons(self, checkButton):
    #     for button in self.group.buttons():
    #         if button == checkButton:
    #             print(self.button_by_data[button])
        # if self.filename != "":
        #     self.excel = ReadExcel(self.filename)
        # for button in self.group.buttons():
        #     if button is not radioButton:
        #         button.setChecked(False)
        #     else:
        #         match self.button_by_data[radioButton]:                        
        #             case 1:
        #                 word = '.xlsx'
        #                 pattern = f"{'data'}/*{word}*"
        #                 file = glob.glob(pattern)
        #                 self.radio = 1
        #                 self.filename = file[0]
        #                 self.basic_file = file[0]
        #                 self.excel = ReadExcel(self.filename)
        #                 self.data.excel = self.excel.getAllLists()
        #                 row = self.tableWidget.rowCount()-1
        #                 self.tableWidget.removeRow(row)

        #                 row = self.tableWidget.rowCount()
        #                 self.tableWidget.insertRow(row)
        #                 self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Максимальное давление сгорания; pz1"))
        #                 try:
        #                     value = str(self.data.excel['pz1'])
        #                 except:
        #                     value = ""
        #                 self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
        #                 self.tableWidget.setItem(row, 1, QTableWidgetItem("Мпа"))

        #                 row = self.tableWidget.rowCount()
        #                 self.tableWidget.insertRow(row)
        #                 self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Давление на впуске; pn1"))
        #                 try:
        #                     value = str(self.data.excel['pn1'])
        #                 except:
        #                     value = ""
        #                 self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
        #                 self.tableWidget.setItem(row, 1, QTableWidgetItem("Мпа"))

        #                 row = self.tableWidget.rowCount()
        #                 self.tableWidget.insertRow(row)
        #                 self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Скорость вращения коленвала; nn"))
        #                 try:
        #                     value = str(self.data.excel['nn'])
        #                 except:
        #                     value = ""
        #                 self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
        #                 self.tableWidget.setItem(row, 1, QTableWidgetItem("Об/мин"))
        #             case 2:
        #                 self.pushButton.setEnabled(True)
        #                 self.radio = 2
        #                 if not os.path.isdir('data/mode1'):
        #                     os.mkdir('data/mode1')
        #                     shutil.copy(self.basic_file, 'data/mode1')
        #                     word = '.xlsx'
        #                     pattern = f"{'data/mode1'}/*{word}*"
        #                     file = glob.glob(pattern)
        #                     self.filename = file[0][:-5] + "_res.xlsx"
        #                     os.rename(file[0], self.filename)
        #                 else:
        #                     word = '.xlsx'
        #                     pattern = f"{'data/mode1'}/*{word}*"
        #                     file = glob.glob(pattern)
        #                     self.filename = file[0]
        #                 self.excel = ReadExcel(self.filename)
        #                 self.data.excel = self.excel.getAllLists()
        #                 row = self.tableWidget.rowCount()-1
        #                 self.tableWidget.removeRow(row)
        #                 row = self.tableWidget.rowCount()-1
        #                 self.tableWidget.removeRow(row)
        #                 row = self.tableWidget.rowCount()-1
        #                 self.tableWidget.removeRow(row)

        #                 row = self.tableWidget.rowCount()
        #                 self.tableWidget.insertRow(row)
        #                 self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Скорость вращения коленвала; nn"))
        #                 try:
        #                     value = str(self.data.excel['nn'])
        #                 except:
        #                     value = ""
        #                 self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
        #                 self.tableWidget.setItem(row, 1, QTableWidgetItem("Об/мин"))
        #             case 3:
        #                 self.pushButton.setEnabled(True)
        #                 self.data.excel = self.excel.getAllLists()
        #                 self.radio = 3
        #                 if not os.path.isdir('data/mode2'):
        #                     os.mkdir('data/mode2')
        #                     shutil.copy(self.basic_file, 'data/mode2')
        #                     word = '.xlsx'
        #                     pattern = f"{'data/mode2'}/*{word}*"
        #                     file = glob.glob(pattern)
        #                     self.filename = file[0][:-5] + "_res.xlsx"
        #                     os.rename(file[0], self.filename)
        #                 else:
        #                     word = '.xlsx'
        #                     pattern = f"{'data/mode2'}/*{word}*"
        #                     file = glob.glob(pattern)
        #                     self.filename = file[0]
        #                 self.excel = ReadExcel(self.filename)
        #                 self.data.excel = self.excel.getAllLists()
        #                 row = self.tableWidget.rowCount()-1
        #                 self.tableWidget.removeRow(row)
        #                 row = self.tableWidget.rowCount()-1
        #                 self.tableWidget.removeRow(row)
        #                 row = self.tableWidget.rowCount()-1
        #                 self.tableWidget.removeRow(row)

        #                 row = self.tableWidget.rowCount()
        #                 self.tableWidget.insertRow(row)
        #                 self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Скорость вращения коленвала; nn"))
        #                 try:
        #                     value = str(self.data.excel['nn'])
        #                 except:
        #                     value = ""
        #                 self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
        #                 self.tableWidget.setItem(row, 1, QTableWidgetItem("Об/мин"))
        #             case 4:
        #                 self.pushButton.setEnabled(True)
        #                 self.data.excel = self.excel.getAllLists()
        #                 self.radio = 4
        #                 if not os.path.isdir('data/mode3'):
        #                     os.mkdir('data/mode3')
        #                     shutil.copy(self.basic_file, 'data/mode3')
        #                     word = '.xlsx'
        #                     pattern = f"{'data/mode3'}/*{word}*"
        #                     file = glob.glob(pattern)
        #                     self.filename = file[0][:-5] + "_res.xlsx"
        #                     os.rename(file[0], self.filename)
        #                 else:
        #                     word = '.xlsx'
        #                     pattern = f"{'data/mode3'}/*{word}*"
        #                     file = glob.glob(pattern)
        #                     self.filename = file[0]
        #                 self.excel = ReadExcel(self.filename)
        #                 self.data.excel = self.excel.getAllLists()
        #                 row = self.tableWidget.rowCount()-1
        #                 self.tableWidget.removeRow(row)
        #                 row = self.tableWidget.rowCount()-1
        #                 self.tableWidget.removeRow(row)
        #                 row = self.tableWidget.rowCount()-1
        #                 self.tableWidget.removeRow(row)

        #                 row = self.tableWidget.rowCount()
        #                 self.tableWidget.insertRow(row)
        #                 self.tableWidget.setVerticalHeaderItem(row, QTableWidgetItem("Скорость вращения коленвала; nn"))
        #                 try:
        #                     value = str(self.data.excel['nn'])
        #                 except:
        #                     value = ""
        #                 self.tableWidget.setItem(row, 0, QTableWidgetItem(value))
        #                 self.tableWidget.setItem(row, 1, QTableWidgetItem("Об/мин"))
        # self.tableWidget.blockSignals(False)
        # self.tableWidget.cellChanged.connect(self.cellChange)
    def cellChange(self, row, column):
        key = self.tableWidget.verticalHeaderItem(row).text().split('; ')[1]
        if validation(self.tableWidget.item(row, column).text(), key, self.data.excel):
            self.tableWidget.blockSignals(True)
            self.tableWidget.item(row, column).setBackground(QtGui.QColor(255,255,0))
            self.tableWidget.blockSignals(False)
            self.data.excel[key] = self.tableWidget.item(row, column).text()
            # self.excel = ReadExcel(self.filename)
            # self.excel.updateCell(key, self.tableWidget.item(row, column).text(), self.filename)
            if key == 'pn1':
                self.data.excel['pn2'] = self.tableWidget.item(row, column).text()
                self.excel.updateCell('pn2', self.tableWidget.item(row, column).text(), self.filename)
        else:
            self.tableWidget.blockSignals(True)
            self.tableWidget.item(row, column).setBackground(QtGui.QColor(255,0,0))
            self.tableWidget.blockSignals(False)

    def clear(self):
        self.expert.modes = []
        self.expert.dict_mode1.clear()
        self.expert.dict_mode2.clear()
        self.expert.dict_mode3.clear()
        self.expert.input_mode1 = 0
        self.expert.input_mode2 = 0
        self.expert.input_mode3 = 0
        self.err.error_dict.clear()
        self.lineEdit.setText("")
        self.lineEdit.setEnabled(False)
        self.lineEdit_2.setText("")
        self.lineEdit_2.setEnabled(False)
        self.lineEdit_3.setText("")
        self.lineEdit_3.setEnabled(False)
        self.lineEdit_4.setText("")
        self.lineEdit_4.setEnabled(False)
        self.lineEdit_5.setText("")
        self.lineEdit_5.setEnabled(False)
        self.lineEdit_6.setText("")
        self.lineEdit_6.setEnabled(False)
        self.lineEdit_7.setText("")
        self.lineEdit_7.setEnabled(False)
        self.lineEdit_8.setText("")
        self.lineEdit_8.setEnabled(False)
        self.lineEdit_9.setText("")
        self.lineEdit_9.setEnabled(False)
        self.lineEdit_10.setText("")
        self.lineEdit_10.setEnabled(False)
        self.lineEdit_11.setText("")
        self.lineEdit_11.setEnabled(False)
        self.lineEdit_12.setText("")
        self.lineEdit_12.setEnabled(False)
        self.radioButton.setChecked(False)
        self.radioButton.setEnabled(False)
        self.radioButton_2.setChecked(False)
        self.radioButton_2.setEnabled(False)
        self.radioButton_3.setChecked(False)
        self.radioButton_3.setEnabled(False)
        self.radioButton_4.setChecked(False)
        self.radioButton_4.setEnabled(False)
        self.radioButton_5.setChecked(False)
        self.radioButton_5.setEnabled(False)
        self.radioButton_6.setChecked(False)
        self.radioButton_6.setEnabled(False)
        self.checkBox.setChecked(False)
        self.checkBox_2.setChecked(False)
        self.checkBox_3.setChecked(False)

    def phase_change(self, state):
        if state:
            self.expert.change_phase = True
            with open('data/app.json') as file:
                datafile = json.load(file)
            datafile['phase'] = True
            if 'input_mode1' in datafile:
                if datafile['input_mode1'] == 2:
                    excel = Excel(glob.glob('data/mode1/*.xlsx')[0])
                    arr = excel.ParseFromFile()
                    excel.ParseToFile(self.phase_shift_360(arr), glob.glob('data/mode1/*.xlsx')[0])
            if 'input_mode2' in datafile:
                if datafile['input_mode2'] == 2:
                    excel = Excel(glob.glob('data/mode2/*.xlsx')[0])
                    arr = excel.ParseFromFile()
                    excel.ParseToFile(self.phase_shift_360(arr), glob.glob('data/mode2/*.xlsx')[0])
            if 'input_mode3' in datafile:
                if datafile['input_mode3'] == 2:
                    excel = Excel(glob.glob('data/mode3/*.xlsx')[0])
                    arr = excel.ParseFromFile()
                    excel.ParseToFile(self.phase_shift_360(arr), glob.glob('data/mode3/*.xlsx')[0])     
            with open('data/app.json', 'w') as file:
                json.dump(datafile, file)
        else:
            with open('data/app.json') as file:
                datafile = json.load(file)
            datafile['phase'] = False
            self.expert.change_phase = False
            with open('data/app.json', 'w') as file:
                json.dump(datafile, file)

    def phase_shift_360(self, massive2):
        size = len(massive2) // 2
        y = []
        y_first_sector = []
        y_second_sector = []
        for i in range(size):
            y_first_sector.append(massive2[i])
        for i in range(size):
            y_second_sector.append(massive2[size+i])
        y.extend(y_second_sector)
        y.extend(y_first_sector)
        return y